package it.koutifaris.travianmap.view.villagedetails;

import it.koutifaris.travianmap.R;
import it.koutifaris.travianmap.model.Tribe;
import it.koutifaris.travianmap.model.Village;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

public class VillageDetailsDialog extends DialogFragment {

	private static class Arguments {
		private static Village village;
	}

	private VillageDetailsView villageView;

	public static void storeParameters(Village tappedVillage) {
		Arguments.village = tappedVillage;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(R.layout.village_details_dialog, null);

		Village village = Arguments.village;
		initView(view, village);

		builder.setView(view)
				.setPositiveButton(R.string.village_details_close, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						dialog.dismiss();
					}
				});

		return builder.create();
	}

	private void initView(View view, Village village) {
		villageView = new VillageDetailsViewImpl(view);

		villageView.setVillageName(village.getName());
		villageView.setCoords(village.getX(), village.getY());
		villageView.setPopulation(village.getPopulation());
		villageView.setPlayerName(village.getOwner().getName());
		villageView.setTribe(village.getOwner().getTribe());
		villageView.setAllianceName(village.getOwner().getAlliance().getName());

		villageView.setOnPlayerClickListener(new OnPlayerClickListener(village.getOwner(), this));
	}

	public interface VillageDetailsView {

		void setVillageName(String villageName);

		void setCoords(int x, int y);

		void setPopulation(int population);

		void setPlayerName(String playerName);

		void setTribe(Tribe tribe);

		void setAllianceName(String allianceName);

		void setOnPlayerClickListener(OnClickListener clickListener);
	}

}
